<template>
  <div class="app-container home">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="patientName">
        <el-input
            v-model="queryParams.patientName"
            placeholder="请输入姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="床号" prop="visitBed">
        <el-input
            v-model="queryParams.visitBed"
            placeholder="请输入姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <div class="bod-card" v-loading.body="loading">
      <el-card class="box-card" v-for="(info, index) in infoList" :key="info.visitId">
        <div slot="header" class="clearfix header overhide">
          <span>床位 {{ info.visitBed }}</span>
          <span>&nbsp;|&nbsp; {{ info.deptName }}</span>
          <div class="items">
            <span>{{ info.patientName }}&nbsp;</span>
            <span>&nbsp;|&nbsp; {{ selectDictLabel(sys_user_sex, info.patientSex) }}&nbsp;</span>
            <span>&nbsp;|&nbsp; {{ info.visitAge }}&nbsp;</span>
            <el-button class="button" type="text" style="float: right" @click="unBindDevice(info.pdiId)">解除绑定</el-button>
          </div>
        </div>
        <div slot="footer" style="margin-top: 5px">
          <span>电量:{{ info.pdiCurElectricity }}%&nbsp;</span>
          <span>&nbsp;|&nbsp;已佩戴 {{ info.pdiCurDuration }}&nbsp;</span>
          <span>&nbsp;|&nbsp;温度 {{ info.pdiCurTemperature }}&nbsp;</span>
        </div>
      </el-card>
    </div>

    <!-- 添加或修改设备信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="患者" prop="visitId">
          <el-select v-model="form.visitId" placeholder="请选择患者">
            <el-option
                v-for="item in patientList"
                :key="item.visitId"
                :label="item.patientName"
                :value="item.visitId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceCode">
          <el-select v-model="form.deviceCode" placeholder="请选择设备">
            <el-option
                v-for="item in deviceList"
                :key="item.deviceCode"
                :label="item.deviceCode"
                :value="item.deviceCode"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Index">
import { listInfo, unBind, addBind, unBindPatientList, unBindDeviceList } from "@/api/pages/bind/bind";

const version = ref('3.8.7')
const { proxy } = getCurrentInstance();

const { sys_user_sex, patient_visit_status } = proxy.useDict("sys_user_sex", "patient_visit_status");

const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const patientList = ref([]);
const deviceList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: null,
    patientName: null,
    visitBed: null,
    visitCode: null
  },
  unBindPatientParams: {
    deptId: null,
    patientName: null,
    visitBed: null,
    visitCode: null
  },
  unBindDeviceParams: {
    deviceCode: null,
    deviceModel: null
  },
  rules: {
    visitId: [
      { required: true, message: "就诊ID不能为空", trigger: "blur" }
    ],
    deviceCode: [
      { required: true, message: "设备编号不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, unBindPatientParams, unBindDeviceParams, form, rules } = toRefs(data);

/** 查询设备信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
  getUnPatientList();
  getUnDeviceList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 表单重置
function reset() {
  form.value = {
    visitId: null,
    deviceCode: null
  };
  proxy.resetForm("infoRef");
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加绑定";
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      addBind(form.value).then(respose => {
        proxy.$modal.msgSuccess("绑定成功");
        open.value = false;
        getList();
        getUnPatientList();
        getUnDeviceList();
      });
    }
  });
}

/** 解除绑定*/
function unBindDevice(pdiId) {
  console.log("传值是:", pdiId)
  unBind(pdiId).then(response => {
    proxy.$modal.msgSuccess("解除绑定成功");
    getList();
    getUnPatientList();
    getUnDeviceList();
  });
}

/** 查询未绑定的患者列表*/
function getUnPatientList() {
  unBindPatientList(unBindPatientParams.value).then(response => {
    patientList.value = response.data;
  });
}

/** 查询未绑定的设备列表*/
function getUnDeviceList() {
  unBindDeviceList(unBindDeviceParams.value).then(response => {
    deviceList.value = response.data;
  });
}

getList();
getUnPatientList();
getUnDeviceList();
</script>

<style scoped lang="scss">

.bod-card {
  display: flex;
  flex-wrap: wrap;
}
.box-card {
  width: 25%;
  margin: 15px 0 0 15px;
  &.item {
    margin-top: 10px;
  }
}

.header {
  padding: 10px 0px;
  border-bottom: 1px solid #ebeef5;
  box-sizing: border-box;
}
</style>


